const express=require('express');
const router=express.Router();
const query = require('../mysql')
router.get('/getMenu',function(req,res){
        console.log(req.body);
        var  sql = `SELECT * FROM my_route`;
        query(sql,function (err,result,) {
            if(err){ //sql 语句报错
                res.send({code:202,data:err,msg:"错误"})
                return;
            }
            if(result.length==0){
                res.send({code:201,data:result,msg:"ok"})
            }else{
                res.send({code:200,data:result,msg:"ok"})
            }
        })
});
router.post('/addMenu',function(req,res){
  console.log(req.body);
  const data = req.body
  var  sql = `INSERT INTO my_router (pid, id, name, type, path, component)
      VALUES ('${data.pid}', '${data.id}', '${data.name}', '${data.type}', '${data.path}', '${data.component}');`;
  query(sql,function (err,result,) {
      if(err){ //sql 语句报错
          res.send({code:202,data:err,msg:"错误"})
          return;
      }
      if(result.length==0){
          res.send({code:201,data:result,msg:"ok"})
      }else{
          res.send({code:200,data:result,msg:"ok"})
      }
  })
});
router.post('/delMenu',function(req,res){
  console.log(req.body);
  const data = req.body
  var  sql = `DELETE FROM my_router WHERE id = '${data.id}'`;
  query(sql,function (err,result,) {
      if(err){ //sql 语句报错
          res.send({code:202,data:err,msg:"错误"})
          return;
      }
      if(result.length==0){
          res.send({code:201,data:result,msg:"ok"})
      }else{
          res.send({code:200,data:result,msg:"ok"})
      }
  })
});
module.exports = router
